<?php
$objXajax->registerFunction("msfrmdailyreport_load");
$objXajax->registerFunction("msfrmdailyreport_productDetail");

function msfrmdailyreport_load($reporttype=1,$dateFrom='',$dateTo=''){ 
    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $addPath = '_'.$reporttype;
    $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
    $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
    $datasum = array();
    $arrData = msfrmdailyreport_createArray($datasum,$reporttype,$dateFrom,$dateTo);     
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/ka_dailyreport_grid.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('msfrmdailyreport_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('msfrmdailyreport_tbl');");  
    $objResponse->addScript('changeWaiting(1);');                      
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function msfrmdailyreport_createArray(&$datasum,$reporttype,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix;
    $groupby = " group by DATE_FORMAT(h.timeend,'%Y-%m-%d') order by  DATE_FORMAT(h.timeend,'%Y-%m-%d') ";
    if($reporttype==2){
     $groupby = " group by h.room_id order by h.room_id ";
    }
    elseif($reporttype==3){
      $groupby = ' group by h.user_update order by h.user_update ' ; 
    }
    
    $sql = " select r.name as roomname,DATE_FORMAT(h.timeend,'%Y-%m-%d') as timeend,r.id as room_id, h.generate_id,
     u.username as username,sum(h.price) as price,sum(h.totalprice) as totalprice   
     from tbl_history_book_room$tbl_postfix as h join tbl_room$tbl_postfix r ON h.room_id = r.id join tbl_user as u ON u.id = h.user_update 
     where datediff('$dateFrom',h.timeend)<=0 and datediff('$dateTo',h.timeend)>=0 $groupby ";//
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $totalsum = 0;
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $arr[$i]['productprice']= $arr[$i]['totalprice'] - $arr[$i]['price'] ;      
          $totalsum  += (int)$arr[$i]['totalprice'] ;
          $pricesum  += (int)$arr[$i]['price'] ;
     } 
         
    $datasum[0]['totalprice']= $totalsum;
    $datasum[0]['price']= $pricesum;
    $datasum[0]['productprice']= $totalsum-$pricesum;
    return  $arr  ;
} 

function msfrmdailyreport_productDetail($generate_id){
    global $objDbUpdate,$lang,$tbl_postfix,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = " select name as productname,num as num,price as price from tbl_history_product$tbl_postfix where generate_id = '$generate_id'  ";//
   // echo $sql; die();
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $pricesum  += (int)$arr[$i]['price'] ;
     }
     $datasum[0]['price']= $pricesum;
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_reportgeneral_productdetail_grid.html');
    $oTbs->MergeBlock('blk_data', $arr); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('msfrmdailyreport_productDetail', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("$('#msfrmdailyreport_productDetail').dialog('open');");
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax             
    
} 

# end region process data
?>
